package com.utils;


import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yangziqiang on 16-8-10.
 */
public class JdbcUtil {
    public JdbcUtil() {
    }


    public static Object[] convertParams(Object[] objs, String fieldName) throws Exception {
        ArrayList list = new ArrayList();
        Object[] var6 = objs;
        int var5 = objs.length;

        for(int var4 = 0; var4 < var5; ++var4) {
            Object object = var6[var4];
            Field f = object.getClass().getDeclaredField(fieldName);
            f.setAccessible(true);
            list.add(f.get(object));
        }
        return list.toArray();
    }

    @SuppressWarnings(value="unchecked")
    public static <T> T[] convertParams(Object[] objs, String fieldName, Class<T> clazz) throws Exception {
        ArrayList list = new ArrayList();
        Object[] var7 = objs;
        int var6 = objs.length;

        for(int var5 = 0; var5 < var6; ++var5) {
            Object object = var7[var5];
            Field f = object.getClass().getDeclaredField(fieldName);
            f.setAccessible(true);
            list.add(f.get(object));
        }

        return (T[]) list.toArray((Object[])Array.newInstance(clazz, 0));
    }


    public static Map beans2MapList(List objs, String fieldName) {
        HashMap map = new HashMap();

        try {
            Iterator var4 = objs.iterator();

            while(var4.hasNext()) {
                Object e = var4.next();
                Field f = e.getClass().getDeclaredField(fieldName);
                f.setAccessible(true);
                String key = "";
                if(f.get(e) instanceof Double) {
                    key = String.valueOf(((Double)f.get(e)).intValue());
                } else {
                    key = f.get(e).toString();
                }

                if(!map.containsKey(key)) {
                    ArrayList values = new ArrayList();
                    values.add(e);
                    map.put(key, values);
                } else {
                    ((List)map.get(key)).add(e);
                }
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return map;
    }
    public static Date newDate(String strtime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date =null;
        try {
            date= sdf.parse(strtime);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return date;
    }


    public static Map beans2Map(List objs, String fieldName) {
        HashMap map = new HashMap();

        try {
            Iterator var4 = objs.iterator();

            while(var4.hasNext()) {
                Object e = var4.next();
                Field f = e.getClass().getDeclaredField(fieldName);
                f.setAccessible(true);
                if(f.get(e) instanceof Double) {
                    Double key = (Double)f.get(e);
                    map.put(String.valueOf(key.intValue()), e);
                } else {
                    map.put(f.get(e).toString(), e);
                }
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return map;
    }

    /**
     * 传递翻页参数
     * @param count     总记录条数
     * @param pageNum   当前页码
     * @param pageSize  每页条数
     * @param list      每页的数据
     * @return
     */
    public static Page newPage(Integer count,Integer pageNum,Integer pageSize,List list){
        Page page = new Page();
        page.setCount(count);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        int totalPage=(int) Math.ceil(Double.valueOf(count)/pageSize);
        page.setTotalPage(totalPage);
        page.setListData(list);
        return page;
    }


}
